﻿using MediatR;
using Production_Get.API.DTO;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.Produce.Order;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Application.Commands.OrderShow
{
    /// <summary>
    /// 销售订单列表
    /// </summary>
    public class OrdersScretenHandler : IRequestHandler<OrdersScretenCommend, Result>
    {
        private readonly IBaseRepositoryGet<OrdersDto> OrdersDto;

        public OrdersScretenHandler(IBaseRepositoryGet<OrdersDto> OrdersDto)
        {
            this.OrdersDto = OrdersDto;
        }

        public async Task<Result> Handle(OrdersScretenCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };

            string sql = "SELECT * FROM Orders a INNER JOIN OrdersType b on a.OrdersTypeId=b.OrdersTypeId INNER JOIN Project c on a.ProjectId=c.ProjectId" +
                " INNER JOIN Customer d on a.CustomerId=d.CustomerId INNER JOIN Department e on a.DepartmentId=e.DepartmentId " +
                "INNER JOIN WorkflowInstance f on a.OrdersNo=f.FormFields INNER JOIN WorkflowAuditor g on f.WorkflowDefinitionId=g.WorkflowDefinitionId " +
                "&& f.WorkflowInstanceId=g.WorkflowId where a.IsDelete=0";
            OrdersDto come = new OrdersDto();
            var list = OrdersDto.GetAll(sql, come).ToList();

            if (!string.IsNullOrEmpty(request.OrdersNo))
            {
                list = list.Where(x => x.OrdersNo.Contains(request.OrdersNo)).ToList();
            }

            if (!string.IsNullOrEmpty(request.OrdersName))
            {
                list = list.Where(x => x.OrdersName.Contains(request.OrdersName)).ToList();
            }

            if (request.OrdersTypeId > 0)
            {
                list = list.Where(x => x.OrdersTypeId == request.OrdersTypeId).ToList();
            }

            if (request.CustomerId > 0)
            {
                list = list.Where(x => x.CustomerId == request.CustomerId).ToList();
            }

            if (request.ProjectId > 0)
            {
                list = list.Where(x => x.ProjectId == request.ProjectId).ToList();
            }

            if (request.OrdersState != null)
            {
                list = list.Where(x => x.OrdersState == request.OrdersState).ToList();
            }

            var totalcount = list.Count();
            var pagecount = (int)Math.Ceiling(totalcount * 1.0 / request.pageSize);
            list = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();

            var lief = new
            {
                totalcount,
                pagecount,
                list,
            };

            if (list != null)
            {
                result.Code = (int)CommonErrorCodes.Success;
                result.Data = lief;
                result.Msg = "显示成功";
            }
            else
            {
                result.Msg = "显示失败";
            }
            return result;
        }
    }
}
